package com.konjac.service.impl;

import com.konjac.dao.mapper.SaleOrderMapper;
import com.konjac.pojo.SaleOrder;
import com.konjac.pojo.ServiceState;
import com.konjac.until.Handle;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class TotalServiceImpl {
    //统计: 根据销售情况输出统计的报表。一般内容为每月的销售总额、销售总量及排行榜
    //查询销售表并按每个月的salePrice和saleNumber之和来排名
    public Map<String, Double> sortByPrice() throws IOException {
        //加载mybaits核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        SaleOrderMapper saleOrderMapper = sqlSession.getMapper(SaleOrderMapper.class);

        //执行查询sql并返回List
        Map<String, Double> priceMap = new HashMap<>();
        List<SaleOrder> saleOrderList = saleOrderMapper.selectAll();

        //处理数据库数据List
        for (SaleOrder i : saleOrderList) {
            String saleMonth = Handle.getSaleMonth(i.getSaleTime());
            Double salePrice = i.getSalePrice();
            if(priceMap.get(saleMonth)==null) {
                priceMap.put(saleMonth,salePrice);
            }
            else {
                priceMap.put(saleMonth,salePrice + priceMap.get(saleMonth));
            }
        }

        //排序
        priceMap = Handle.sortMap01(priceMap);

        //System.out.println(priceMap);

        //将map提交回controller
        return priceMap;
    }

    public  Map<String,Integer> sortByNumber() throws IOException {
        //加载mybaits核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        SaleOrderMapper saleOrderMapper = sqlSession.getMapper(SaleOrderMapper.class);

        //执行查询sql并返回List
        Map<String, Integer> numberMap = new HashMap<>();
        List<SaleOrder> saleOrderList = saleOrderMapper.selectAll();

        //处理数据库数据List
        for (SaleOrder i : saleOrderList) {
            String saleMonth = Handle.getSaleMonth(i.getSaleTime());
            Integer saleNumber = i.getSaleNumber();
            if(numberMap.get(saleMonth)==null) {
                numberMap.put(saleMonth,saleNumber);
            }
            else {
                numberMap.put(saleMonth,saleNumber + numberMap.get(saleMonth));
            }
        }

        //排序
        numberMap = Handle.sortMap02(numberMap);

        //System.out.println(priceMap);

        sqlSession.close();
        //将map提交回controller
        return numberMap;
    }


}
